#include "fig_exploding_manager.h"
#include "fig_exploding_collection.h"


FIGExplodingManager&	FIGExplodingManager::Instance()
{
	static FIGExplodingManager	Inst;
	return Inst;
}

FIGExplodingManager::FIGExplodingManager()
{
	// initialize offscreen
	for(unsigned int i = 0; i < FIGExploding::EXPLODING_TYPES_COUNT; i++)
	{
		Spawn((FIGExploding::EXPLODING_TYPES)i, math::Vec2(1.5f,1.5f), 1.0f, 0);
	}
}

FIGExplodingManager::~FIGExplodingManager()
{

}

void		FIGExplodingManager::Render(const math::Vec2& Offset,float Scale)
{
	for(unsigned int i = 0; i < m_ExplodingCollections.size(); i++)
	{
		FIGExplodingCollection* pCol = m_ExplodingCollections[i];
		pCol->Render(Offset,Scale);
	}
}
	
void		FIGExplodingManager::RenderDebug(const math::Vec2& Offset,float Scale)
{
	for(unsigned int i = 0; i < m_ExplodingCollections.size(); i++)
	{
		FIGExplodingCollection* pCol = m_ExplodingCollections[i];
		pCol->RenderDebug(Offset,Scale);
	}
}


void		FIGExplodingManager::Tick(float dt)
{
	for(unsigned int i = 0; i < m_ExplodingCollections.size(); i++)
	{
		FIGExplodingCollection* pCol = m_ExplodingCollections[i];
		pCol->Tick(dt);
	}
}


void		FIGExplodingManager::Clear()
{
	for(unsigned int i = 0; i < m_ExplodingCollections.size(); i++)
	{
		FIGExplodingCollection* pCol = m_ExplodingCollections[i];
		pCol->Clear();
	}

	for( int i = (int)m_ExplodingCollections.size() - 1; i >= 0; i--)
	{
		FIGExplodingCollection* pCol = m_ExplodingCollections[i];
		UTIL_SAFE_DELETE(pCol);
	}
	m_ExplodingCollections.resize(0);
}

void		FIGExplodingManager::Spawn(FIGExploding::EXPLODING_TYPES Type, 
											const math::Vec2& Pos,
											float SizeScale,
											float Rotation											
											)
{
	bool Found = false;
	for( int i = (int)m_ExplodingCollections.size() - 1; i >= 0; i--)
	{
		FIGExplodingCollection* pCol = m_ExplodingCollections[i];
		if ( pCol->GetType() == Type)
		{
			pCol->Spawn(Pos, SizeScale, Rotation);
			Found = true;
		}		
	}

	if(!Found)
	{
		FIGExplodingCollection* pCol = new FIGExplodingCollection(Type);
		m_ExplodingCollections.push_back(pCol);
		pCol->Spawn(Pos, SizeScale, Rotation);
	}

			
}

